package com.gitee.hljdrl.dialog;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;

import com.gitee.hljdrl.dialog.basis.DialogEvent;
import com.gitee.hljdrl.dialog.basis.ViewPartVisitor;
import com.gitee.hljdrl.dialog.util.UIUtil;


/**
 * 如果宽度是占满全屏的话就会不展示入场动画。
 * https://blog.csdn.net/haha223545/article/details/119034830
 */
public class UIDialog extends Dialog implements DialogEvent {

    //------------------------------------------
    private String TAG = "UIDialog";
    private boolean mFixAnim = true;
    private int mWindowAnimations = 0;
    //------------------------------------------
    @LayoutRes
    protected int mLayoutId = 0;

    protected ViewPartVisitor mViewPartVisitor;
    //------------------------------------------
    private View mView;
    //------------------------------------------

    public UIDialog(Context context) {
        this(context, R.style.UIDialogNoAnim);
    }

    public UIDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layout = mLayoutId;
        if (mViewPartVisitor != null && mViewPartVisitor.getLayout() != 0) {
            layout = mViewPartVisitor.getLayout();
        }
        View contentView = View.inflate(getContext(), layout, null);
        mView = contentView;
        setContentView(contentView);
        initView(contentView);
    }

    @Override
    public View getView() {
        return mView;
    }

    private void init() {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    private void initView(View contentView) {
        if (mViewPartVisitor != null) {
            try {
                mViewPartVisitor.onViewVisit((Dialog)this, contentView);
            } catch (Exception ex) {
                //TODO
            }
            int gravity = mViewPartVisitor.getGravity();
            if (gravity == Gravity.TOP) {
                Window window = getWindow();
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                layoutParams.gravity = Gravity.TOP;
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setAttributes(layoutParams);
            }
        }
    }

    @Override
    public UIDialog setLayoutId(int layoutId) {
        mLayoutId = layoutId;
        return this;
    }

    public UIDialog setViewPartVisitor(ViewPartVisitor partVisitor) {
        this.mViewPartVisitor = partVisitor;
        return this;
    }

    public UIDialog setFixAnim(boolean fixAnim) {
        this.mFixAnim = fixAnim;
        return this;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.i(TAG, "onWindowFocusChanged-->" + hasFocus);
        if (mFixAnim) {
            if (hasFocus) {
                if (mWindowAnimations != 0) {
                    getWindow().getDecorView().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            getWindow().setWindowAnimations(mWindowAnimations);
                        }
                    }, 300);
                }
            } else {
                mWindowAnimations = getWindow().getAttributes().windowAnimations;
                getWindow().setWindowAnimations(0);
            }
        }
    }

    private long mShowTime;

    @Override
    public void show() {
        super.show();
        mShowTime = System.currentTimeMillis();
        if (mViewPartVisitor != null) {
            mViewPartVisitor.onShow();
        }
    }

    protected void dismissAsDelayed() {
        Log.i(TAG, "dismissAsDelayed");
        if (UIUtil.isShowing(UIDialog.this)) {
            try {
                if (mViewPartVisitor != null) {
                    mViewPartVisitor.onDismiss(this);
                }
            } catch (Exception ex) {
                //TODO
                ex.printStackTrace();
            }
            super.dismiss();
        }
    }

    @Override
    public void dismiss() {
        if (mViewPartVisitor != null) {
            if (mViewPartVisitor.getMinimumTime() == 0) {
                mViewPartVisitor.onDismiss(this);
            } else {
                try {
                    long mMinimumTime = mViewPartVisitor.getMinimumTime();
                    long time = System.currentTimeMillis() - mShowTime;
                    if (time < mMinimumTime) {
                        long delayMillis = mMinimumTime - time;
                        View view = getView();
                        if (view != null && delayMillis > 0) {
                            view.postDelayed(() -> {
                                dismissAsDelayed();
                            }, delayMillis);
                            return;
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (mViewPartVisitor.onDismiss(this)) {
                return;
            }
        }
        super.dismiss();
    }


//    @Override
//    public boolean onTouchEvent(@NonNull MotionEvent event) {
//        L.i(TAG, "onTouchEvent-->", event.getAction());
//        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
//            // 在这里你可以选择不执行任何操作，从而防止点击外部区域关闭Dialog
//            return true; // 返回true表示已处理事件
//        }
//        return super.onTouchEvent(event);
//    }
}
